package com.project.code.spider.service.impl;

import com.project.code.spider.entity.po.SpiderPlatform;
import com.project.code.spider.mapper.SpiderPlatformMapper;

import com.project.code.spider.service.SpiderPlatformService;

import com.project.code.spider.entity.params.SpiderPlatformPageParams;
import com.project.code.spider.entity.po.SpiderPlatform;
import com.project.code.spider.mapper.SpiderPlatformMapper;
import com.project.code.spider.service.SpiderPlatformService;

import com.project.common.constant.NumberConstants;
import com.project.common.enums.DelFlagEnum;
import com.project.common.utils.ServletUtils;
import com.project.common.utils.bean.CopyObjectUtils;
import com.project.common.utils.number.NumberUtils;
import com.project.framework.web.service.TokenService;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;

import java.util.Date;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * AI调价-爬虫平台 Service业务层处理
 *
 * @author WangYan
 * @date 2023-05-14
 */
@Slf4j
@Service
public class SpiderPlatformServiceImpl implements SpiderPlatformService  {

    @Resource
    private SpiderPlatformMapper spiderPlatformMapper;
    @Resource
    private TokenService tokenService;

    @Override
    public int insert(SpiderPlatform po) {
        po.setId(null);
        po.setCreateTime(new Date());
        po.setCreateUser(tokenService.getLoginUser(ServletUtils.getRequest()).getUsername());
        return spiderPlatformMapper.insert(po);
    }

    @Override
    public int updateById(SpiderPlatform po) {
        if (NumberUtils.isEntity(po.getId())) {
            throw new RuntimeException("数据ID不存在");
        }
        if (delete(Lists.newArrayList(po.getId())) > NumberConstants.ZERO) {
            return insert(po);
        }
        return NumberConstants.ZERO;
    }

    @Override
    public int delete(List<Long> ids) {
        int deleteCount = 0;
        for (Long id : ids) {
            deleteCount = deleteCount + spiderPlatformMapper.updateById(SpiderPlatform.builder()
                    .id(id)
                    .updateTime(new Date())
                    .updateUser(tokenService.getLoginUser(ServletUtils.getRequest()).getUsername())
                    .delFlag(DelFlagEnum.HAS_DELETED.getCode())
                    .build());
        }
        return deleteCount;
    }

    @Override
    public <Vo> Vo read(Long id, Class<Vo> resultClass) {
        SpiderPlatform data = spiderPlatformMapper.selectById(id);
        if (data == null) {
            throw new RuntimeException("根据id查询不到数据");
        }
        return CopyObjectUtils.copyAtoB(data, resultClass);
    }

    @Override
    public <Vo> List<Vo> list(SpiderPlatformPageParams params, Class<Vo> resultClass) {
        LambdaQueryWrapper<SpiderPlatform> wrapper = Wrappers.lambdaQuery(SpiderPlatform.class);
        wrapper.eq(SpiderPlatform::getDelFlag, DelFlagEnum.NOT_DELETED.getCode());
        //TODO 前端的条件查询
        List<SpiderPlatform> page = spiderPlatformMapper.selectList(wrapper);
        return CopyObjectUtils.copyAListToBList(page, resultClass);
    }
}
